/**
 * ymm56.com Inc.
 * Copyright (c) 2013-2021 All Rights Reserved.
 */
package com.algorithm.array.sort;

import java.util.Arrays;

/**
 *  滑动窗口
 *
 *  给定一个数组 nums 和滑动窗口的大小 k，请找出所有滑动窗口里的最大值。
 *
 * 示例:
 *
 * 输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3
 * 输出: [3,3,5,5,6,7]
 * @author 韩繁文
 * @version $Id: huadongAlgorithm.java, v 0.1 2021-04-23 13:06 韩繁文 Exp $$
 */
public class huadongAlgorithm {

    /**
     * 滑动窗口左边下标【0；nums-k】；右边下标【K；nums】
     * @param nums
     * @param k
     * @return
     */

    public static int [] cacul(int[ ]nums,int k){
        if (k == 1) {
            return nums;
        }
        int[] result  = new int[nums.length - k +1];
        for (int i =0; i <= nums.length-k;i++){
            int max = Integer.MIN_VALUE;
            for (int j = i; j<= i + k-1;j++){
                if(max <= nums[j]){
                    max = nums[j];
                }
            }
            result[i]= max;
        }
        return result;
    }


    public static void main(String[] args) {
        System.out.println(Arrays.toString(cacul(new int[]{1,3,-1,-3,5,3,6,7},3)));  //[3, 3, 5, 5, 6, 7]

    }
}
